package com.rpc;

import com.oildatacollection.server.Server;
import com.oildatacollection.util.YamlLoader;
import lombok.extern.slf4j.Slf4j;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Map;

@Slf4j
public class RpcServer {
    private Server ser;

    public RpcServer(Server ser) {
        this.ser = ser;
    }

    public void start() {
        // 获取相应的name
        YamlLoader yamlLoader = new YamlLoader("app-config/application-rpc.yml");
        Map<String, Object> res = (Map<String, Object>) yamlLoader.get("server");
        int port = (int) res.get("port");
        String name = (String) res.get("name");
        try {
            LocateRegistry.createRegistry(port);
            OilInfoSend oilInfoSend = new OilInfoSendImpl(ser);
            Naming.rebind(name, oilInfoSend);
        } catch (RemoteException | MalformedURLException e) {
            log.error("RpcServer初始化失败", e);
            return;
        }
        log.trace("RpcServer启动成功");
    }
}
